/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.annotations.Incomplete;
import com.android1.amarena2d.annotations.NotThreadsave;
import com.android1.amarena2d.commons.Callback;

import java.util.ArrayList;
import java.util.HashMap;


@NotThreadsave
@Incomplete
public class ChannelSystem {

    HashMap<String, Channel> channelMap = new HashMap<String, Channel>();


    public <V> Channel<V> getChannel(String name) {
        Channel<V> channel = channelMap.get(name);
        if (channel == null) {
            channel = new Channel<V>();
            channelMap.put(name, channel);
        }
        return channel;
    }


    public static class Channel<V> {

        ArrayList<Callback<V>> listenerList = new ArrayList<Callback<V>>();
        V value;

        public void write(V value) {
            this.value = value;
            notifyListener(value);
        }

        public void register(Callback<V> listener) {
            listenerList.add(listener);
        }

        public void unregister(Callback<V> listener) {
            listenerList.remove(listener);
        }

        public V getValue() {
            return value;
        }

        void notifyListener(V value) {
            for (int i = 0; i < listenerList.size(); i++) {
                final Callback<V> callback = listenerList.get(i);
                callback.on(value);

            }
        }


    }

}
